﻿CREATE PROCEDURE [acms].[User_Put]
		@Id int,
		@FirstName varchar(50),
		@LastName varchar(50),
		@DateOfBirth datetime,
		@EmailAddress varchar(100),
		@VersionId timestamp,
		@IdOut int output,
		@VersionIdOut timestamp output
AS

IF EXISTS (SELECT null FROM Users WHERE Id = @Id and Deleted = 0) BEGIN
	UPDATE Users
	SET
		FirstName = @FirstName,
		LastName = @LastName,
		DateOfBirth = @DateOfBirth,
		EmailAddress = @EmailAddress
	WHERE
		Id = @Id
		AND VersionId = @VersionId
	if @@rowcount = 0
		return -100
END
ELSE BEGIN
	if @Id is not null and @Id <> 0
		return -200
	else begin
		INSERT Users
			(FirstName, LastName, DateOfBirth, EmailAddress)
		VALUES
			(@FirstName, @LastName, @DateOfBirth, @EmailAddress)
		Set @Id = @@Identity
	end
END

SELECT @IdOut = Id, @VersionIdOut = VersionId FROM Users WHERE Id = @Id

RETURN 0;